use dta\DataSet_v28.dta, clear
rename last_year last_year_run_nat
gen electedXyear=elected*year
egen last_year_win_nat=max(electedXyear), by(pid_lcd)
replace last_year_win_nat=. if last_year_win_nat==0
collapse last_year*, by(pid_lcd)
drop *LCD
save dta\DataSet_v28_last_year.dta, replace

use "dta\LCD_v13.dta" , clear
keep if mayor_loc==1 & year<2023
merge m:1 pid_lcd using "dta\DataSet_v28_last_year.dta"

gen national_run=0
replace national_run=1 if last_year_run_nat>year & last_year_run_nat!=.

gen national_win=0
replace national_win=1 if last_year_win_nat>year & last_year_win_nat!=.

preserve
collapse national*, by(year female)

tw (scatter national_run year  if female==0, msymbol(S) mcolor(gray) connect(l) lpattern(solid) lcolor(black)) ///
   (scatter national_run year  if female==1, msymbol(O) mcolor(black) connect(l) lpattern(solid) lcolor(black)), ///
title("") xtitle("Election year") ytitle("Fraction") title("Appear on future national list") legend(off)  xscale(range(1971 2019)) xlabel(1971(8)2019, labsize(small)) ylabel(0(0.2)0.8, angle(0)) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s2mono) text(0.25 2011 "Men", color(gray)) text(0.7 2011 "Women", color(black)) yline(0.8, lstyle(foreground) lcolor(gs14))
graph save figures/gph/national_run.gph, replace

tw (scatter national_win year  if female==0, msymbol(S) mcolor(gray) connect(l) lpattern(solid) lcolor(black)) ///
   (scatter national_win year  if female==1, msymbol(O) mcolor(black) connect(l) lpattern(solid) lcolor(black)), ///
title("") xtitle("Election year") ytitle("Fraction") title("Win future national office") legend(off)  xscale(range(1971 2019)) xlabel(1971(8)2019,  labsize(small)) ylabel(0(0.05)0.2, angle(0)) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s2mono) text(0.02 2011 "Men", color(gray)) text(0.14 2011 "Women", color(black)) yline(0.2, lstyle(foreground) lcolor(gs14))
graph save figures/gph/national_win.gph, replace
restore


foreach var in national_run national_win {
		estimates clear
	
		forval i = 1971(4)2019{
			eststo diff_`i': qui reg `var' female if year==`i', r
		}
		
		
	coefplot diff_*, scheme(plotplain) vertical aseq swapnames drop(*cons*) ///
		yline(0, lcol(gray) lpattern(dash) lwidth(medthick))  ///
		xlab(1 "1971" 2 " " 3 "1979" 4 " " 5 "1987" 6 " " 7 "1995" 8 " " 9 "2003" 10 " " 11 "2011" 12 " " 13 "2019") ///
		ytitle(Gender gap estimate (w-m)) ///
		xtitle(Election year) ///
		msym(O) mcol(gs4) ciopts(col(gs4)) ///
		legend(off) title(Coefficient plot, size(large)) plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white))
		graph save figures/gph/_`var'_cp.gph, replace
		
		
}

cd figures/gph/
graph combine national_run.gph national_win.gph _national_run_cp.gph _national_win_cp.gph, scheme(s2mono) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white))
graph export ..\FigureA6.pdf, replace
cd ../../
